

#import "APCardStackAnimationCell.h"
#import "APLoginManager.h"
@interface APCardStackAnimationCell ()



@end

@implementation APCardStackAnimationCell


- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
            self.layer.cornerRadius = 8;
            self.layer.masksToBounds = true;
            UIImageView *backgroundImageView = [[UIImageView alloc] init];
            backgroundImageView.userInteractionEnabled = true;
            backgroundImageView.image = [UIImage imageNamed:@"video_card_bg"];
            [self.contentView addSubview:backgroundImageView];
            [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView);
            }];
            
            _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_moreButton setBackgroundImage:[UIImage imageNamed:@"video_more_icon"] forState:UIControlStateNormal];
            [self.contentView addSubview:_moreButton];
            [_moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.contentView).offset(-14);
                make.top.equalTo(self.contentView).offset(18);
                make.size.mas_equalTo(CGSizeMake(28, 28));
            }];
            
            _upvoteButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_upvoteButton setBackgroundImage:[UIImage imageNamed:@"video_upvote_n"] forState:UIControlStateNormal];
            [_upvoteButton setBackgroundImage:[UIImage imageNamed:@"video_upvote_h"] forState:UIControlStateSelected];
            [self.contentView addSubview:_upvoteButton];
            [_upvoteButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(_moreButton.mas_left).offset(-24);
                make.centerY.equalTo(_moreButton.mas_centerY);
                make.size.mas_equalTo(CGSizeMake(28, 28));
            }];
            
            _avatarControl = [[APAvatarControl alloc] init];
            _avatarControl.followButton.hidden = true;
            _avatarControl.avatarImageView.layer.cornerRadius = 18;
            [self.contentView addSubview:_avatarControl];
            [_avatarControl mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).offset(18);
                make.left.equalTo(self.contentView).offset(14);
            }];
            [_avatarControl.avatarImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(36, 36));
            }];
            
            _nameLabel = [[UILabel alloc] init];
            _nameLabel.textColor = [UIColor whiteColor];
            _nameLabel.font = [LYFont fontWithSize:14 fontName:LYFontNamepfr];
            _nameLabel.textAlignment = NSTextAlignmentLeft;
            [self.contentView addSubview:_nameLabel];
            [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(_avatarControl);
                make.left.equalTo(_avatarControl.mas_right).offset(8);
                make.right.equalTo(_upvoteButton.mas_left).offset(-8);
            }];
            
            _titleLabel = [[UILabel alloc] init];
            _titleLabel.textColor = [UIColor whiteColor];
            _titleLabel.font = [LYFont fontWithSize:16 fontName:LYFontNamepfr];
            _titleLabel.textAlignment = NSTextAlignmentLeft;
            [self addSubview:_titleLabel];
            [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_avatarControl.mas_bottom).offset(16);
                make.left.equalTo(self.contentView).offset(14);
                make.right.equalTo(self.contentView).offset(-14);
            }];
            
            _coverImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.contentView addSubview:_coverImageButton];
            [_coverImageButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_titleLabel.mas_bottom).offset(16);
                make.left.right.bottom.equalTo(self.contentView);
            }];
        
        _coverImageView = [[UIImageView alloc] init];
        _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImageView.clipsToBounds = true;
        [_coverImageButton addSubview:_coverImageView];
        [_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_coverImageButton);
        }];
            
            _playImageView = [[UIImageView alloc] init];
            _playImageView.image = [UIImage imageNamed:@"video_play_icon"];
            [self.contentView addSubview:_playImageView];
            [_playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(_coverImageButton);
                make.size.mas_equalTo(CGSizeMake(40, 40));
            }];
    }
    return self;
}


- (void)setModel:(APVideoModel *)model {
    _model = model;
    [_avatarControl.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    _nameLabel.text = model.nickname;
    _upvoteButton.selected = model.isUpvote;
    _titleLabel.text = model.title;
    BOOL isSelf = [model.userId isEqualToString:[[APLoginManager shareInstance] userInfo].userId];
    _moreButton.hidden = isSelf;
    _upvoteButton.hidden = isSelf;
    if (model.isCreate) {
        UIImage *image =  [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",images_path,model.middleThumbUrl]];
        _coverImageView.image = image;
    }else{
        [_coverImageView sd_setImageWithURL:[NSURL URLWithString:model.middleThumbUrl]];
    }
}

@end
